package com.mainService.dubbo.filter;

import com.alibaba.dubbo.rpc.*;
import com.projectCommon.bean.DubboInfoContext;
import com.projectCommon.util.commonUtil.SerializationUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2024/12/01/9:04
 * @target 心若有所向往，何惧道阻且长
 */
@Slf4j
public class BaseFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        DubboInfoContext dubboInfoContext = new DubboInfoContext();
        dubboInfoContext.setTraceId("22002");
        dubboInfoContext.setUserID("808080");
        dubboInfoContext.setConsumer("wy");
        dubboInfoContext.setShopName("武建平 便利店");
        dubboInfoContext.setShopSmokeGearLv("9");
        dubboInfoContext.setShopSmokeType("农网");
        log.info("{} invoke {} interface start",dubboInfoContext.getConsumer(),invocation.getMethodName());
        RpcContext.getContext().setAttachment("dubboInfoContext", SerializationUtil.serialize(dubboInfoContext));
        long start = System.currentTimeMillis();
        // 执行服务调用
        Result result = invoker.invoke(invocation);
        long end = System.currentTimeMillis();
        // 服务调用后输出日志
        log.info("{} invoke {} interface end, cost {}",
                dubboInfoContext.getConsumer(),
                invocation.getMethodName(),
                end-start);
        return result;
    }
}
